package com.ruoyi.mall.app.controller;


import  com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.app.service.DtsOrderService;

import com.ruoyi.mall.common.domain.DtsOrder;
import com.ruoyi.mall.common.domain.DtsOrderGoods;
import com.ruoyi.mall.common.domain.DtsOrdersVo;
import com.ruoyi.mall.common.domain.DtsRefundzTrace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.annotation.*;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/order/myOrder")
public class DtsOrderController extends BaseController {

    @Autowired
    private DtsOrderService dtsOrderService;

    //查询订单
    @GetMapping("/orderList")
    public AjaxResult list(DtsOrder dtsOrder)
    {
        List<DtsOrder> list = dtsOrderService.selectMyOrder(dtsOrder);
        if (list != null){
            return success(list);
        }else {
            return error();
        }
    }
    //订单详情
    @GetMapping("/detail")
    public AjaxResult detail(DtsOrder dtsorder)
    {
        List<DtsOrder> list = dtsOrderService.selectOrderDetail(dtsorder);
        if (list != null){
            return success(list);
        }else {
            return error();
        }
    }

    //修改订单
    @GetMapping("/quOrder")
    public AjaxResult edit(DtsOrder dtsOrder)
    {
        return toAjax(dtsOrderService.quOrder(dtsOrder));
    }

    //删除订单，将deleted=1
    @GetMapping("/delOrder")
    public AjaxResult del(int id)
    {
        return toAjax(dtsOrderService.delOrder(id));
    }


    // 生成订单
    @GetMapping("/createOrder")
    public AjaxResult createOrder(DtsOrder dtsOrder) throws UnsupportedEncodingException, JsonProcessingException {
        dtsOrder.setConsignee(URLDecoder.decode(dtsOrder.getConsignee(), StandardCharsets.UTF_8.toString()));
        dtsOrder.setMobile(URLDecoder.decode(dtsOrder.getMobile(), StandardCharsets.UTF_8.toString()));
        dtsOrder.setAddress(URLDecoder.decode(dtsOrder.getAddress(), StandardCharsets.UTF_8.toString()));
//        dtsOrder.setOrderGoodsList(URLDecoder.decode(dtsOrder.getOrderGoodsList(), StandardCharsets.UTF_8.toString()));
        // 处理 orderGoodsList 字段
        String orderGoods = dtsOrder.getOrderGoods();
        if (orderGoods != null) {
            // 仅当有值时进行解码操作，避免重复解码问题
            orderGoods = URLDecoder.decode(orderGoods, StandardCharsets.UTF_8.toString());
            ObjectMapper objectMapper = new ObjectMapper();
            // 直接指定目标类型为 List<DtsOrderGoods>.class
            List<DtsOrderGoods> dtsOrderGoodsList = objectMapper.readValue(orderGoods, new com.fasterxml.jackson.core.type.TypeReference<List<DtsOrderGoods>>() {});
            dtsOrder.setOrderGoodsList(dtsOrderGoodsList);
        }
        return success(dtsOrderService.createOrder(dtsOrder));
    }


    //退款时将新增一条退款信息
    @GetMapping("/addRefund")
    public AjaxResult addRefund(
            @RequestParam Long orderId,
            @RequestParam String orderSn,
            @RequestParam String refundReason,
            @RequestParam Integer hasPicture,
            @RequestParam String picUrls,
            @RequestParam Integer status,
            @RequestParam String mobile) {

        // 手动构建对象
        DtsRefundzTrace trace = new DtsRefundzTrace();
        trace.setOrderId(orderId);
        trace.setOrderSn(orderSn);
        trace.setRefundReason(refundReason);
        trace.setHasPicture(hasPicture);
        trace.setPicUrls(picUrls);
        trace.setStatus(status);
        trace.setMobile(mobile);

        return toAjax(dtsOrderService.addRefund(trace));
    }
}


























